package com.campusify.service.impl;

import org.springframework.stereotype.Service;
import com.campusify.compents.PageResult;
import com.campusify.pojo.SortExample;
import com.campusify.pojo.SortExample.Criteria;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campusify.mapper.SortMapper;
import com.campusify.pojo.Sort;
import com.campusify.service.SortService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SortServiceImpl implements SortService {

	@Autowired
	private SortMapper sortMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<Sort> findAll() {
		return sortMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<Sort> page=   (Page<Sort>) sortMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(Sort sort) {
		sortMapper.insert(sort);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(Sort sort){
		sortMapper.updateByPrimaryKey(sort);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public Sort findOne(Integer sortID){
		return sortMapper.selectByPrimaryKey(sortID);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] sortIDs) {
		for(Integer sortID:sortIDs){
			sortMapper.deleteByPrimaryKey(sortID);
		}		
	}
	
	
		@Override
	public PageResult findPage(Sort sort, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		SortExample example=new SortExample();
		Criteria criteria = example.createCriteria();
		
		if(sort!=null){			
						if(sort.getSortname()!=null && sort.getSortname().length()>0){
				criteria.andSortnameLike("%"+sort.getSortname()+"%");
			}	
		}
		
		Page<Sort> page= (Page<Sort>)sortMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
